<?php
class ControllerDelimanPhieugiaobao extends Controller
{
	private $error = array();
	private $data_khuvuquanly = array();
   	function __construct() 
	{	
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		$this->data['list_button'] = array();
		foreach($listchek as $method)
		{
			$this->data['list_button'][$method] = $this->user->hasPermission($this->getRoute(), $method);
		}
	
	 	$this->load->model("deliman/phieugiaobao");
		$this->load->model("deliman/phieutrabao");
		$this->load->model("deliman/phieuphanphoi");
		$this->load->model("deliman/nhanvien");
		$this->load->model("deliman/khuvuc");
		$this->load->model("deliman/loaibao");
		$this->load->model("deliman/kybao");
		$this->load->model("common/control");
		$this->data['loaibaos'] = $this->model_deliman_loaibao->getList();
		
		$nhanvienid = $this->user->getNhanVien();
		$where = " AND nhanvienphutrachid = '".$nhanvienid."'";
		$this->data_khuvuquanly = $this->model_deliman_khuvuc->getList($where);
		
   	}
	
	public function getCBNhanVienGaoBao()
	{
		$nhanvienid = $this->request->get['nhanvienid'];
		$datas = $this->getNhanVienGiaoBao($nhanvienid);
		$this->data['output'] = '<option value="">Chọn tất cả</option>'; 
		$this->data['output'] .=  $this->model_common_control->getDataCombobox($datas, 'hoten', 'nhanvienid');
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();	
	}
	
	private function getNhanVienGiaoBao($nhanvienid)
	{
		//Lay cac khu vuc minh quan ly
		if(trim($nhanvienid) != '')
		{
			$where = " AND nhanvienphutrachid = '".$nhanvienid."'";
		}
		$data_khuvucquanly = $this->model_deliman_khuvuc->getList($where);
		$arr_khuvucquanly = $this->string->matrixToArray($data_khuvucquanly,'khuvucid');
		//Nhung nhan vien giao bao trong khu vuc cua minh
		$where = " AND khuvucid in ('". implode("','",$arr_khuvucquanly)."') AND tinhtrang <> 'deleted'";
		$data_phutrachkuvuc = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where);
		$arr_nhanvienphutrachid = $this->string->matrixToArray($data_phutrachkuvuc,'nhanvienphutrachid');
		$where = " AND loainhanvienid = '3'";
		$where .= " AND nhanvienid in ('". implode("','",$arr_nhanvienphutrachid) ."')";
		$data_nhanvien = $this->model_deliman_nhanvien->getList($where);
		return $data_nhanvien;
	}
	
	public function getNhanVienGiaoBaoTheoKhuVuc()
	{
		$khuvucid = $this->request->get['khuvucid'];
		//Nhung nhan vien giao bao trong khu vuc cua minh
		$where = " AND khuvucid = '".$khuvucid."' AND tinhtrang <> 'deleted'";
		$data_phutrachkuvuc = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where);
		$arr_nhanvienphutrachid = $this->string->matrixToArray($data_phutrachkuvuc,'nhanvienphutrachid');
		$where = " AND loainhanvienid = '3'";
		$where .= " AND nhanvienid in ('". implode("','",$arr_nhanvienphutrachid) ."')";
		$data_nhanvien = $this->model_deliman_nhanvien->getList($where);
		$this->data['output'] = '<option value="">Chọn tất cả</option>'; 
		$this->data['output'] .=  $this->model_common_control->getDataCombobox($data_nhanvien, 'hoten', 'nhanvienid');
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function getNhanVienQuanLyKhuVuc($nhanvienid)
	{
		//Lay cac khu vuc minh quan ly
		$where = " AND nhanvienphutrachid = '".$nhanvienid."'";
		$data_khuvucquanly = $this->model_deliman_khuvuc->getList($where);
		//$arr_khuvucquanly = $this->string->matrixToArray($data_khuvucquanly,'khuvucid');
		//Lay nhung khu vuc con
		$arr_nhanvienquanlyid = array();
		foreach($data_khuvucquanly as $khuvuc)
		{
			$data_khuvuccong = array();
			$this->model_deliman_khuvuc->getTree($khuvuc['khuvucid'],$data_khuvuccong);
			foreach($data_khuvuccong as $khuvuccon)
				$arr_nhanvienquanlyid[] = $khuvuccon['nhanvienphutrachid'];
		}
		
		$where = " AND loainhanvienid = '2'";
		$where .= " AND nhanvienid in ('". implode("','",$arr_nhanvienquanlyid) ."')";
		$data_nhanvien = $this->model_deliman_nhanvien->getList($where);
		return $data_nhanvien;
	}
	
	public function index()
	{
		$this->getList();
	}
	
	public function insert()
	{
    	$this->getForm();
	}
	
	public function update()
	{
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['disabled'] = 'disabled="disabled"';
		$this->data['class'] = 'readonly';
		$this->getForm();
	
  	}
	public function view()
	{
		$this->data['item'] = $this->model_deliman_phieugiaobao->getItem($this->request->get['phieugiaobaoid']);
		
		$arr_tenkhuvuc = $this->model_deliman_khuvuc->getPath($this->data['item']['khuvucgiaoid'],"tenkhuvuc");
		
		$this->data['item']['tenkhuvucgiao'] = implode(" - ",$arr_tenkhuvuc);
		$where = " AND phieugiaobaoid = '".$this->data['item']['phieugiaobaoid']."' AND soluong > 0";
		
		$this->data['data_ctphieuphanphoi'] = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
		/*$arr_loaibaos = $this->string->matrixToArray($this->data['data_ctphieuphanphoi'],'loaibaoid');
		$where = " AND loaibaoid in ('". implode("','",$arr_loaibaos) ."')";
		$this->data['data_loaibaos'] = $this->model_deliman_loaibao->getList($where);
		
		foreach($this->data['data_ctphieuphanphoi'] as $key => $item)
		{
			$tongdagiao = $this->tinhTongKyBaoDaGiaoBao($item['kybaoid']);
			$tonchitieu = $this->tinhTongKyBaoChiTieu($item['kybaoid']);
			$this->data['data_ctphieuphanphoi'][$key]['tongdagiao'] = $tongdagiao - $item['soluong'];
			$this->data['data_ctphieuphanphoi'][$key]['soluongton'] = $tonchitieu - $tongdagiao;
			$this->data['data_ctphieuphanphoi'][$key]['tongchitieu'] = $tonchitieu;
		}*/
		$this->id='content';
		$this->template="deliman/phieugiaobao_view.tpl";
		if($data['print'] == "true")
			$this->layout="layout/print";
		else
			$this->layout="layout/deliman";
		
		$this->render();
  	}
	
	public function delete() 
	{
		$listctphieugiaobaoid=$this->request->post['delete'];
		
		if(count($listctphieugiaobaoid))
		{
			foreach($listctphieugiaobaoid as $id)
			{
				print_r($id);
				$this->model_deliman_phieugiaobao->deleteChiTietPhieuGiaoBao($id);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('deliman/phieugiaobao/insert');
		$this->data['delete'] = $this->url->http('deliman/phieugiaobao/delete');		
		
		$this->data['nhanvienquanlykhuvucs'] = $this->getNhanVienQuanLyKhuVuc($this->user->getNhanVien());
		$arr_nhanvienid = $this->string->matrixToArray($this->data['nhanvienquanlykhuvucs'],"nhanvienid");
		$data = $this->request->get;
		$where = " AND soluong > 0 ";
		$where .= " AND nhanvienquanlyid in ('". implode("','",$arr_nhanvienid) ."')";
		if (trim($data['tungay']) != "")
		{
			$where .= " AND ngaylap >= '".$this->date->formatViewDate($data['tungay'])."'";	
			
		}
		if (trim($data['denngay']) != "")
		{
			$where .= " AND ngaylap < '".$this->date->formatViewDate($data['denngay'])." 24:00:00'";	
		}
		if($data['nhanvienquanlyid'] != "")
		{
			$where .= " AND nhanvienquanlyid = '".$data['nhanvienquanlyid']."'";
		}
		if($data['nhanviengiaobaoid'] != "")
		{
			$where .= " AND nhanviengiaobaoid = '".$data['nhanviengiaobaoid']."'";
		}
		
		if($data['loaibaoid'] != "")
		{
			$where .= " AND loaibaoid = '".$data['loaibaoid']."'";
		}
		if($data['kybaoid'] != "")
		{
			$where .= " AND kybaoid = '".$data['kybaoid']."'";
		}
		$where .= " ORDER BY  `ngaylap` DESC ";
	
		$this->data['datas'] = array();	
		
		$rows = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/phieugiaobao/update&phieugiaobaoid='.$this->data['datas'][$i]['phieugiaobaoid']);
			$this->data['datas'][$i]['text_edit'] = "Edit";
			
			$this->data['datas'][$i]['link_phanphoibao'] = $this->url->http('deliman/phieugiaobao/phanphoibao&phieugiaobaoid='.$this->data['datas'][$i]['phieugiaobaoid']);
			$this->data['datas'][$i]['text_phanphoibao'] = "Phân phối báo";
			
			$phieugiaobao = $this->model_deliman_phieugiaobao->getItem($this->data['datas'][$i]['phieugiaobaoid']);
			$this->data['datas'][$i]['ngaylap'] = $phieugiaobao['ngaylap'];
			$this->data['datas'][$i]['maphieugiaobao'] = $phieugiaobao['maphieugiaobao'];
			$this->data['datas'][$i]['tennhanvienquanly'] = $phieugiaobao['tennhanvienquanly'];
			$this->data['datas'][$i]['tennhanviengiaobao'] = $phieugiaobao['tennhanviengiaobao'];
			
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->data['data_khuvuquanly'] = $this->data_khuvuquanly;
		
		
		foreach($this->data['data_khuvuquanly']  as $key => $khuvuc)
		{
			$data_baodangphathanh = $this->layTinhTangBaoTheoKhuVuc($khuvuc['khuvucid']);
			$this->data['data_khuvuquanly'][$key]['kybaos'] = $data_baodangphathanh;
			
			
		}
		
		
		
		$this->id='content';
		$this->template="deliman/phieugiaobao_list.tpl";
		$this->layout="layout/deliman";
		$this->render();
	}
	
	public function layTinhTangBaoTheoKhuVucForm()
	{
		$khuvucid = $this->request->get['khuvucid'];
		$phieugiaobaoid = $this->request->get['phieugiaobaoid'];
		$data_tinhtrangbao = $this->layTinhTangBaoTheoKhuVuc($khuvucid);
		foreach($data_tinhtrangbao as $key => $item)
		{
			//So luong giao cua phieu
			if($phieugiaobaoid != "")
			{
				$where = " AND phieugiaobaoid = '".$phieugiaobaoid."' AND kybaoid = '".$item['kybaoid']."'";
				$where .= " AND khuvucgiaoid = '".$khuvucid."'";
				$data_ctphieugiaobao = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
				
				
				$tongsoluonggiaocuakybao = 0;
				foreach($data_ctphieugiaobao as $ct)
				{
					$tongsoluonggiaocuakybao += $ct['soluong'];	
				}
				$data_tinhtrangbao[$key]['soluong'] = $tongsoluonggiaocuakybao;
				$data_tinhtrangbao[$key]['id'] = $data_ctphieugiaobao[0]['id'];
			}
			//So bao da giao o cac phieu truoc
			$where = " AND phieugiaobaoid <> '".$phieugiaobaoid."' AND kybaoid = '".$item['kybaoid']."'";
			$where .= " AND khuvucgiaoid = '".$khuvucid."'";
			$data_ctphieugiaobao = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
			
			$tongsoluonggiaocuakybao = 0;
			foreach($data_ctphieugiaobao as $ct)
			{
				$tongsoluonggiaocuakybao += $ct['soluong'];	
			}
			$data_tinhtrangbao[$key]['tongdagiao'] = $tongsoluonggiaocuakybao;
		}
		$arr_loaibao = $this->string->matrixToArray($data_tinhtrangbao,'loaibaoid');
		$where = " AND loaibaoid in ('". implode("','",$arr_loaibao) ."')";
		$this->data['data_loaibaos'] = $this->model_deliman_loaibao->getList($where);
		$this->data['data_tinhtrangbao'] = $data_tinhtrangbao;
		
		//print_r($data_tinhtrangbao);
		
		$this->id='content';
		$this->template="deliman/phieugiaobao_tinhtrangbao.tpl";
		$this->render();
	}
	
	private function layTinhTangBaoTheoKhuVuc($khuvucid)
	{
		
		$data_baodangphathanh = $this->getKyBaoDangPhatHanh();
		
		
		foreach($data_baodangphathanh as $key => $kybao)
		{
			$data_baoduocpp = $this->layBaoDuocPhanPhoi($khuvucid,$kybao['kybaoid']);
			$data_baodapp = $this->layBaoDaPhanPhoi($khuvucid,$kybao['kybaoid']);
			$chitieu = 0;
			foreach($data_baoduocpp as $ct)
				$chitieu += $ct['chitieu'];
				
			$dapp = 0;
			foreach($data_baodapp as $ct)
				$dapp+=$ct['chitieu'];
			
			$data_baodangphathanh[$key]['chitieu'] = $chitieu;
			$data_baodangphathanh[$key]['daphanphoi'] = $dapp;
			
			
			$where = " AND kybaoid = '".$kybao['kybaoid']."'";
			$where .= " AND khuvucgiaoid = '".$khuvucid."'";
			$data_ctphieugiaobao = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
			//print_r($data_ctphieugiaobao);
			$tongsoluonggiaocuakybao = 0;
			foreach($data_ctphieugiaobao as $ct)
			{
				$tongsoluonggiaocuakybao += $ct['soluong'];	
			}
			$data_baodangphathanh[$key]['tongdagiao'] = $tongsoluonggiaocuakybao;
			//Lay cac bao nhan tu tra ve tu nhan vien tu nhan vien quan ly khu vuc cap duoi va nhan vien phat hanh bao tai khu vuc do
			$where = " AND kybaoid = '".$kybao['kybaoid']."'";
			$where .= " AND khuvucnhanid = '".$khuvucid."'";
			$where .= " AND nhanviennhanid = '".$this->user->getNhanVien()."'";
			$data_ctphieutrabao = $this->model_deliman_phieutrabao->getChiTietPhieuTraBaoList($where);
			
			$tongsoluongnhan = 0;
			foreach($data_ctphieutrabao as $ct)
			{
				$tongsoluongnhan += $ct['soluongtra'];
			}
			$data_baodangphathanh[$key]['tongsoluongnhan'] = $tongsoluongnhan;
			//Lay so luong bao tra ve cho khu vuc cha
			$where = " AND kybaoid = '".$kybao['kybaoid']."'";
			$where .= " AND khuvuctraid = '".$khuvucid."'";
			$where .= " AND nhanvientraid = '".$this->user->getNhanVien()."'";
			$data_ctphieutrabao = $this->model_deliman_phieutrabao->getChiTietPhieuTraBaoList($where);
			
			$tongsoluongtra = 0;
			foreach($data_ctphieutrabao as $ct)
			{
				$tongsoluongtra += $ct['soluongtra'];
			}
			$data_baodangphathanh[$key]['tongsoluongtra'] = $tongsoluongtra;
			
			$data_baodangphathanh[$key]['soluongton'] = $chitieu + $tongsoluongnhan - $tongsoluongtra - $dapp - $tongsoluonggiaocuakybao ;
			
		}
		//Ko hien thi nhung ky bao dang phat hanh ma ko duoc phan phoi (chitieu = 0)
		/*foreach($data_baodangphathanh as $k => $kybao)
		{
			if($kybao['chitieu'] == 0)
				unset($data_baodangphathanh[$k]);
		}*/
		
		
		
		
		return $data_baodangphathanh;
	}
	
	private function layBaoDuocPhanPhoi($khuvucid,$kybaoid)
	{
		//Tong hop so luong bao duoc phan phoi
		
		//Lay chi tiet phieu phan phoi theo khu vuc
		$arr_khuvucid = $this->string->matrixToArray($this->data_khuvuquanly,'khuvucid');
		
		//echo $where = " AND kybaoid = '".$kybaoid."'";
		$where = " AND khuvucid = '".$khuvucid."' AND kybaoid = '".$kybaoid."'";
		$data_ctphieuphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
		return $data_ctphieuphanphoi;
	}
	
	private function layBaoDaPhanPhoi($khuvucid,$kybaoid)
	{
		
		//Lay cac khu vuc con
		$arr_khuvucconid = array();
		
		$data_khuvuccon = $this->model_deliman_khuvuc->getChild($khuvucid);
		foreach($data_khuvuccon as $khuvuccon)
			$arr_khuvucconid[] = $khuvuccon['khuvucid'];
		$where = " AND khuvucid in ('".implode("','",$arr_khuvucconid)."') AND kybaoid = '".$kybaoid."'";
		$data_ctphieuphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
		return $data_ctphieuphanphoi;
		
	}
	
	private function layBaoDaGiao($khuvucid)
	{
		$where = " AND khuvucgiaoid = '".$khuvucid."'";
		$data_ctphieugiao = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
		return $data_ctphieugiao;
	}
	
	public function getKyBaoDangPhatHanh()
	{
		$where = " AND ngayphathanh > '".$this->date->getToday."' Order by ngayphathanh DESC";
		$data_baodangphathanh = $this->model_deliman_kybao->getList($where);
		foreach($data_baodangphathanh as $key => $item)
		{
			$loaibao = $this->model_deliman_loaibao->getItem($item['loaibaoid']);
			$data_baodangphathanh[$key]['tenloaibao'] = $loaibao['tenloaibao'];
			
		}
		
		return $data_baodangphathanh;
	}
	
	public function tinhTongKyBaoChiTieu($kybaoid)
	{
		$nhanvienid = $this->user->getNhanVien();
		//Lay cac khu vuc phu trach
		$where = " AND nhanvienphutrachid = '".$nhanvienid."'";
		$data_khuvucphutrach = $this->model_deliman_khuvuc->getList($where);
		//Lay chi tiet phieu phan phoi theo khu vuc
		$arr_khuvucid = $this->string->matrixToArray($data_khuvucphutrach,'khuvucid');
		
		$where = " AND kybaoid = '".$kybaoid."'";
		$where .= " AND khuvucid in ('". implode("','",$arr_khuvucid) ."')";
		$data_ctphieuphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
		
		$sum = 0;
		foreach($data_ctphieuphanphoi as $item)
		{
			$sum += $item['chitieu'];	
		}
		
		return $sum;
	}
	
	public function tinhTongKyBaoDaGiaoBao($kybaoid)
	{
		$where = " AND kybaoid = '".$kybaoid."'";
		$nhanvienid = $this->user->getNhanVien();
		$where .= " AND nhanvienquanlyid = '".$nhanvienid."'";
		$data_ctphieugiaobao = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
		$sum = 0;
		foreach($data_ctphieugiaobao as $item)
		{
			$sum += $item['soluong'];	
		}
		return $sum;
	}
	
	private function getForm()
	{		
		$ctphanphoi = array();
		$this->data['data_ctgiaobao'] = array();
		$this->data['nhanviens'] = $this->getNhanVienGiaoBao($this->user->getNhanVien());
		$this->data['data_khuvuquanly'] = $this->data_khuvuquanly;
		if ((isset($this->request->get['phieugiaobaoid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_phieugiaobao->getItem($this->request->get['phieugiaobaoid']);
			
			$where = " AND phieugiaobaoid = '".$this->data['item']['phieugiaobaoid']."'";
			$this->data['data_ctphieuphanphoi'] = $this->model_deliman_phieugiaobao->getChiTietPhieuGiaoBaoList($where);
			$arr_loaibaos = $this->string->matrixToArray($this->data['data_ctphieuphanphoi'],'loaibaoid');
			$where = " AND loaibaoid in ('". implode("','",$arr_loaibaos) ."')";
			$this->data['data_loaibaos'] = $this->model_deliman_loaibao->getList($where);
			
			foreach($this->data['data_ctphieuphanphoi'] as $key => $item)
			{
				$tongdagiao = $this->tinhTongKyBaoDaGiaoBao($item['kybaoid']);
				$tonchitieu = $this->tinhTongKyBaoChiTieu($item['kybaoid']);
				$this->data['data_ctphieuphanphoi'][$key]['tongdagiao'] = $tongdagiao - $item['soluong'];
				$this->data['data_ctphieuphanphoi'][$key]['soluongton'] = $tonchitieu - $tongdagiao;
				$this->data['data_ctphieuphanphoi'][$key]['tongchitieu'] = $tonchitieu;
			}
			//print_r($this->data['data_ctphieuphanphoi']);
    	}
		else
		{
			$this->data['item']['nhanvienquanlyid'] = $this->user->getNhanVien();
			$this->data['item']['ngaylap'] = $this->date->getToday();
			
			
		}
		
		
		$this->id='content';
		$this->template='deliman/phieugiaobao_form.tpl';
		$this->layout="layout/deliman";
		$this->render();
	}
	
	
	
	
	
	public function save()
	{
		$data = $this->request->post;
		
		if($this->validateForm($data))
		{
			$mon = $this->date->numberFormate($this->date->now['mon']);
			$year = $this->date->now['year'];
			$prefix = $mon.$year;
			
			if($data['maphieugiaobao']=="")
				$data['prefix'] ="PGB".$prefix;
			
			$data['ngaylap'] = $this->date->formatViewDate($data['ngaylap']);
			$nhanvien = $this->model_deliman_nhanvien->getItem($data['nhanviengiaobaoid']);
			$data['masonhanviengiaobao'] = $nhanvien['masonhanvien'];
			$data['tennhanviengiaobao'] = $nhanvien['hoten'];
			
			if($data['phieugiaobaoid']=="")
			{
				$data['phieugiaobaoid'] = $this->model_deliman_phieugiaobao->insert($data);	
			}
			else
			{
				$this->model_deliman_phieugiaobao->update($data);	
			}
			//Xoa chi tiet giao bao
			$listdel = split(",", $data['delgiaobao']);
			if(count($listdel))
			{
				foreach($listdel as $id)
				{
					$this->model_deliman_phieugiaobao->deleteChiTietPhieuGiaoBao($id);	
				}
			}
			//Luu chi tiet giao bao
			$arrsoluong = $data['soluong'];
			
			foreach($arrsoluong as $key=>$soluong)
			{
				$ctgiaobao['id'] = $data['id'][$key];
				$ctgiaobao['phieugiaobaoid'] = $data['phieugiaobaoid'];
				$ctgiaobao['kybaoid'] = $data['kybaoid'][$key];
				$kybao = $this->model_deliman_kybao->getItem($ctgiaobao['kybaoid']);
				$ctgiaobao['sokybao'] = $kybao['sokybao'];
				$ctgiaobao['tenkybao'] = $kybao['tenkybao'];
				$ctgiaobao['loaibaoid'] = $data['loaibaoid'][$key];
				$loaibao = $this->model_deliman_loaibao->getItem($ctgiaobao['loaibaoid']);
				$ctgiaobao['tenloaibao'] = $loaibao['tenloaibao'];
				$ctgiaobao['ngayphathanh'] = $kybao['ngayphathanh'];
				$ctgiaobao['soluong'] = $soluong;
				
				$this->model_deliman_phieugiaobao->saveChiTietPhieuGiaoBao($ctgiaobao);
			}
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		/*if (trim($data['maphieugiaobao']) == "") 
		{
      		$this->error['maphieugiaobao'] = "Bạn chưa nhập mã chức năng";
    	}
		else
		{
			$where = " AND maphieugiaobao = '".$data['maphieugiaobao']."'";
			$kybaos = $this->model_deliman_phieugiaobao->getList($where);
			if(count($kybaos) > 0 && $data['phieugiaobaoid'] == "")
			{
				$this->error['maphieugiaobao'] = "Mã chức năng đã tồn tại trong hệ thống";	
			}
		}*/
		
		if (trim($data['maphieugiaobao']) != "") 
		{
			$where = " AND maphieugiaobao = '".$data['maphieugiaobao']."'";
			$kybaos = $this->model_deliman_phieugiaobao->getList($where);
			if(count($kybaos) > 0 && $data['phieugiaobaoid'] == "")
			{
				$this->error['maphieugiaobao'] = "Mã phiếu giao báo đã tồn tại trong hệ thống";	
			}
		}
		if (trim($data['ngaylap']) == "") 
		{
      		$this->error['ngaylap'] = "Bạn chưa chọn ngày lập";
    	}
		if (trim($data['nhanviengiaobaoid']) == 0) 
		{
      		$this->error['nhanviengiaobaoid'] = "Bạn chưa chọn nhân viên giao báo";
    	}
		
		if(count($data['soluong'])==0)
			$this->error['soluong'] = "Bạn chưa nhập số lượng báo giao";
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	public function getKyBao()
	{
		$col = $this->request->get['col'];
		$val = $this->request->get['val'];
		$operator = $this->request->get['operator'];
		$nhanvienid = $this->request->get['nhanvienid'];
		if($operator == "")
			$operator = "equal";
		
		$where = "";
		switch($operator)
		{
			case "equal":
				$where = " AND ".$col." = '".$val."'";
				break;
			case "like":
				$where = " AND ".$col." like '%".$val."%'";
				break;
			case "other":
				$where = " AND ".$col." <> '".$val."'";
				break;
			case "in":
				$where = " AND ".$col." in  (".$val.")";
				break;
			
		}
		$where .= " ORDER BY ngayphathanh DESC";
			
		$datas = $this->model_deliman_kybao->getList($where);
		foreach($datas as $key => $item)
		{
			$loaibao = $this->model_deliman_loaibao->getItem($item['loaibaoid']);
			$datas[$key]['tenloaibao'] = $loaibao['tenloaibao'];
			$datas[$key]['ngayphathanh'] = $this->date->formatMySQLDate($item['ngayphathanh']);
			//Tinh tổng chi tieu
			$this->load->model("deliman/phieuphanphoi");
			
			$where = " AND qlkhuvucid = '". $nhanvienid ."'";
			$where .= " AND kybaoid = '". $datas[$key]['kybaoid'] ."'";
			
			/*$ctphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
			
			$sum = 0;
			foreach($ctphanphoi as $val)
			{
				$sum+=$val['chitieu'];
			}
			$datas[$key]['tongchitieu'] = $sum;*/
			$datas[$key]['tongchitieu'] = $this->tinhTongKyBaoChiTieu($item['kybaoid']);
			$datas[$key]['tongdagiao'] = $this->tinhTongKyBaoDaGiaoBao($item['kybaoid']);
			$datas[$key]['soluongton'] = $datas[$key]['tongchitieu'] - $datas[$key]['tongdagiao'];
		}
		
		
		$this->data['output'] = json_encode(array('kybaos' => $datas));
		$this->id="kybao";
		$this->template="common/output.tpl";
		$this->render();
	}
}
?>